﻿@{
    Layout = null;
    List<caseclassificationtable>? info = ViewData["CaseClassification"] as List<caseclassificationtable>;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>案件分类管理</title>
    <link href="~/layui/css/layui.css" rel="stylesheet" />
    <script src="~/layui/layui.js"></script>
    <script src="~/lib/jquery/dist/jquery.js"></script>

    <style>
        .layui-form-label {
            width: 85px;
        }
    </style>
</head>
<body>
    <div class="Content">
        @* 搜索区域 *@
        <div class="search">
            <div class="layui-form-item">
                <div class="layui-input-group">
                    <input type="text" placeholder="请输入数据字典类型" id="strName" class="layui-input">
                    <div class="layui-input-split layui-input-suffix" style="cursor: pointer;">
                        <i class="layui-icon layui-icon-search" id="btnSearch"></i>
                    </div>
                </div>
            </div>
        </div>

        @* 表格 *@
        <div class="table">
            <table class="layui-hide" id="ID-treeTable-demo"></table>
        </div>
    </div>

    @* 头部操作栏 *@
    <script type="text/html" id="TPL-treeTable-demo">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-sm" lay-event="addInfo">添加</button>
        </div>
    </script>

    @* 操作列 *@
    <script type="text/html" id="TPL-treeTable-demo-tools">
        <div class="layui-btn-container">
          <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">编辑</a>
          <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="addChild">添加</a>
          <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="delete">删除</a>
        </div>
    </script>


    @* 添加/编辑案件类别信息 *@
    <div id="divContent" style="display:none;">
        <form class="layui-form" lay-filter="formInfo">

            @* 案件父类名 *@
            <div class="layui-form-item layui-inline" style="margin-top:20px;width: 100%;">
                <label class="layui-form-label">案件父类名</label>
                <div class="layui-input-inline layui-col-xs12" style="width: 68%;">
                    <select name="CaseParentId" lay-filter="ClassificationId" id="caseType" class="layui-input">
                    </select>
                </div>
            </div>

            @* 案件类型名 *@
            <div class="layui-form-item">
                <label class="layui-form-label">案件类型名</label>
                <div class="layui-input-block">
                    <input type="hidden" name="ClassificationId" />
                    <input type="text" name="CaseTypeName" lay-verify="required" placeholder="请输入案件类型名" autocomplete="off" class="layui-input" style="width:87%">
                </div>
            </div>

            @* 案件级别 *@
            <div class="layui-form-item">
                <label class="layui-form-label">案件级别</label>
                <div class="layui-input-block">
                    <input type="radio" name="CaseLevel" value="false" title="普通" checked>
                    <input type="radio" name="CaseLevel" value="true" title="应急">
                </div>
            </div>

            @* 事件部件标识 *@
            <div class="layui-form-item">
                <label class="layui-form-label">事件部件标识</label>
                <div class="layui-input-block">
                    <input type="radio" name="EventPartIdentification" value="false" title="事件" checked>
                    <input type="radio" name="EventPartIdentification" value="true" title="部件">
                </div>
            </div>

            @* 事件部件编码 *@
            <div class="layui-form-item" style="margin-top:20px;">
                <label class="layui-form-label">事件部件编码</label>
                <div class="layui-input-block">
                    <input type="text" name="EventPartIdentificationId" lay-verify="required" placeholder="请输入事件部件编码" autocomplete="off" class="layui-input" style="width:87%">
                </div>
            </div>

            @* 提交表单 *@
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="submit" class="layui-btn" lay-submit lay-filter="btnSave">保存</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>


    <script>
        var pageIndex;
        var form;
        var treeTable;
        layui.use(function () {
            treeTable = layui.treeTable;
            var layer = layui.layer;
            var dropdown = layui.dropdown;
            form = layui.form;
            // 渲染
            var inst = treeTable.render({
                elem: '#ID-treeTable-demo',
                id: "test",
                url: '/CaseClassification/GetCaseClassificationList',
                toolbar: '#TPL-treeTable-demo',
                cols: [[
                    { type: 'checkbox', fixed: 'left' },
                    { field: 'id', title: '编号', width: 80, sort: true },
                    { field: 'pid', title: '案件父级编号', },
                    { field: 'CaseTypeName', title: '案件类型名', sort: true },
                    {
                        field: 'CaseLevel', title: '案件级别', templet: function (res) {
                            return res.CaseLevel ? "应急" : "普通"
                        }
                    },
                    {
                        field: 'EventPartIdentification', title: '事件部件表示', templet: function (res) {
                            return res.CaseLevel ? "事件" : "部件"
                        }
                    },
                    { fixed: "right", title: "操作", width: 190, align: "center", toolbar: "#TPL-treeTable-demo-tools" }
                ]],
                page: true,
                tree: {
                    view: {
                        showIcon: true,
                        dblClickExpand: true,
                        expandAllDefault: false,
                        icon: "layui-icon-mike",
                        indent: 16
                    }
                }
            });
            // 表头工具栏工具事件
            treeTable.on("toolbar(test)", function (obj) {
                var config = obj.config;
                var tableId = config.id;
                var status = treeTable.checkStatus(tableId);
                // 获取选中行
                if (obj.event === "addInfo") {
                    bindSelect();
                    pageIndex = layer.open({
                        type: 1, // page 层类型
                        area: ['500px', '450px'],
                        title: '添加类型信息',
                        shade: 0.6, // 遮罩透明度
                        shadeClose: false, // 点击遮罩区域，关闭弹层
                        anim: 0, // 0-6 的动画形式，-1 不开启
                        content: $("#divContent"),
                        cancel: function (index, layero, that) {
                            clearForm();
                            layer.close(index);
                            $("#divContent").css("display", "none");
                        }
                    });
                }
            });

            // 单元格工具事件
            treeTable.on('tool(' + inst.config.id + ')', function (obj) {
                var layEvent = obj.event; // 获得 lay-event 对应的值
                var trElem = obj.tr;
                var trData = obj.data;
                var tableId = obj.config.id;
                console.log(trData);
                bindSelect();
                //编辑
                if (layEvent === "edit") {
                    form.val("formInfo", {
                        ClassificationId: trData.id,
                        CaseParentId: trData.pid,
                        CaseTypeName: trData.CaseTypeName,
                        CaseLevel: trData.CaseLevel ? "true" : "false",
                        EventPartIdentification: trData.EventPartIdentification ? "true" : "false",
                        EventPartIdentificationId: trData.EventPartIdentificationId
                    })

                    pageIndex = layer.open({
                        type: 1, // page 层类型
                        area: ['500px', '450px'],
                        title: '添加类型信息',
                        shade: 0.6, // 遮罩透明度
                        shadeClose: false, // 点击遮罩区域，关闭弹层
                        anim: 0, // 0-6 的动画形式，-1 不开启
                        content: $("#divContent"),
                        cancel: function (index, layero, that) {
                            clearForm();
                            layer.close(index);
                            $("#divContent").css("display", "none");
                        }
                    });
                }
                //添加子节点
                else if (layEvent === "addChild") {
                    form.val("formInfo", {
                        CaseParentId: trData.id
                    })

                    pageIndex = layer.open({
                        type: 1, // page 层类型
                        area: ['500px', '450px'],
                        title: '添加类型信息',
                        shade: 0.6, // 遮罩透明度
                        shadeClose: false, // 点击遮罩区域，关闭弹层
                        anim: 0, // 0-6 的动画形式，-1 不开启
                        content: $("#divContent"),
                        cancel: function (index, layero, that) {
                            clearForm();
                            layer.close(index);
                            $("#divContent").css("display", "none");
                        }
                    });
                } else if (layEvent === "delete") {
                    layer.confirm('确定要删除吗？', { icon: 3 }, function () {
                        $.ajax({
                            url: "/CaseClassification/Delete",
                            type: "post",
                            data: { id: trData.id },
                            success: function (res) {
                                if (res.res) {
                                    layer.msg(res.msg, { icon: 1 });
                                    tableReload();
                                } else {
                                    layer.msg(res.msg, { icon: 2 });
                                }
                            }
                        })
                    }, function () {
                        layer.msg('用户取消操作', { icon: 0 });
                    });
                }
            });

            form.on('submit(btnSave)', function (data) {
                var field = data.field; // 获取表单字段值
                console.log(1123);
                $.ajax({
                    url: "/CaseClassification/Save",
                    type: "post",
                    data: field,
                    success: function (res) {
                        if (res) {
                            layer.msg("保存成功!", { icon: 1 });
                            tableReload();
                            clearForm();
                            layer.close(pageIndex);
                        } else {
                            layer.msg("保存失败!", { icon: 2 });
                        }
                    }
                })
                return false; // 阻止默认 form 跳转
            });
        });

        //查询按钮
        $("#btnSearch").click(function () {
            tableReload();
        })


        //重载表格
        function tableReload() {
            treeTable.reloadData('test', {
                where: {
                    strName: $("#strName").val(),
                }
            })
        }

        //清理表单
        function clearForm() {
            form.val("formInfo", {
                ClassificationId: 0,
                CaseParentId: 0,
                CaseTypeName: "",
                CaseLevel: false,
                EventPartIdentification: false,
                EventPartIdentificationId: ""
            })
            $("#divContent").css("display", "none");
        }

        //绑定下拉框数据
        function bindSelect() {
            //绑定前先清除
            $("#caseType option").remove();
            $.ajax({
                url: "/CaseClassification/getCaseClassInfoBindSelect",
                type: "get",
                async:false,
                success: function (res) {
                    $("#caseType").append("<option value='0'' > ---请选择--- </option>");
                    $.each(res, function (index, item) {
                        $("#caseType").append("<option value='" + item.ClassificationId + "'>" + item.CaseTypeName + "</option>");
                    })
                    form.render("select");
                }
            })
        }
    </script>
</body>
</html>
